package com.shine.demo.utils;


import com.alibaba.druid.filter.config.ConfigTools;
import com.alibaba.druid.util.DruidPasswordCallback;

/**
 * 数据库密码解密
 */
public class DruidPasswordUtil extends DruidPasswordCallback {

    private static final String RSA_PUBLIC_KEY = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKTto6+lA6hl5FxRk+v40AbtZJ+c9hYPFUunA2aYL9ZNcKUgv012t6l3M365U/hNrJz4Pt654rtVwKv47nAWIeMCAwEAAQ==";
    private static final String RSA_PRIVATE_KEY = "MIIBVgIBADANBgkqhkiG9w0BAQEFAASCAUAwggE8AgEAAkEApO2jr6UDqGXkXFGT6/jQBu1kn5z2Fg8VS6cDZpgv1k1wpSC/TXa3qXczfrlT+E2snPg+3rniu1XAq/jucBYh4wIDAQABAkEAl9UCBNgoBuZOlcKwJ+O2NzpikSJh1irqkuvdEJz4JRNG8srKRuo0RZE2+256CT717fAeheundZg+mcCzwpO0sQIhAOXdwVXt2kppEOh/oa2oJFfeJhg35+4Xigee7htrStnbAiEAt63eUZeRNh8oLPX/V9VnYbM3PK+ccsPzjnEK9CKUKpkCIQCvUZhA8r24XTQxMA9fRMwbXGsPs6wokzmKoHQY45deWwIgVaZd5O4oaHRe55hOWUbJzmUjfMjmyx1UlTdhRDZboQECIQDaIhidt8g4LVYni3lGNkzNs3UtQ8g+OrbGs6GwZXhDYw==";

    public static void main(String[] args) throws Exception{
        //密码明文
        String password = "nlsrnMFrWfB08GZsZa1IP6mmVoDy8gImCdAU6Vh8UhXRBxI79JkdB6";


        String miwen = encrypt(password);
        System.out.println("私钥加密后=====>" + miwen);
        String mingwen = decrypt(miwen);
        System.out.println("公钥解密后=====>" + mingwen);

    }


    /**
     * 描述 私钥加密
     *
     */
    public static String encrypt(String strMing) {
        try {
            return ConfigTools.encrypt(RSA_PRIVATE_KEY, strMing);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }

    /**
     * 描述 公钥解密
     *
     */
    public static String decrypt(String strMi) {
        try {
            return ConfigTools.decrypt(RSA_PUBLIC_KEY, strMi);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String [] getRsaKey() throws Exception {
        //获取公私钥
        String [] keyPair = ConfigTools.genKeyPair(512);
        String privateKey = keyPair[0];
        String publicKey = keyPair[1];
        System.out.println(privateKey);
        System.out.println(publicKey);
        return keyPair;
    }
}

